/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ljsystem;

import databaseManager.DbConn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 47
 */
public class UserGroupManager {

    public UserGroupManager() {
    }
    public String msg = "";

    public boolean addNewGroup(String name, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = null;
        if (name != null && memo != null) {
            try {
                name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "insert into usergroup(group_name,memo) values";
                sql += "('" + name + "','" + memo + "')";
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "添加成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public bo.UserGroup getGroupById(String id) {
        if (id == null) {
            return null;
        }
        bo.UserGroup g = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
            String sql;
            sql = "select g.usergroup_id,g.group_name,ifnull(g.memo,'') as memo from usergroup g where g.usergroup_id=" + id;
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                g = new bo.UserGroup();
                g.groupId = rs.getLong("usergroup_id");
                g.groupName = rs.getString("group_name");
                g.memo = rs.getString("memo");
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return g;
    }

    public List getAllGroup() {
        List list = new ArrayList();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            String sql;
            sql = "select g.usergroup_id,g.group_name,ifnull(g.memo,'') as memo from usergroup g order by group_name";
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);//获得所有用户组信息
            bo.UserGroup g = null;
            while (rs.next()) {//组织列表
                g = new bo.UserGroup();
                g.groupId = rs.getLong("usergroup_id");
                g.groupName = rs.getString("group_name");
                g.memo = rs.getString("memo");
                list.add(g);
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return list;
    }

    public boolean modifyGroup(String id, String name, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        if (id != null && name != null && memo != null) {
            try {
                id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
                name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "update usergroup";
                sql += " set group_name='" + name + "',";
                sql += "memo='" + memo + "'";
                sql += " where usergroup_id=" + id;
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "修改成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public boolean deleteGroup(String[] gids) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        int i = 0;
        if (gids.length > 0) {
            try {
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                conn.setAutoCommit(false);
                for (i = 0; i < gids.length; i++) {
                    gids[i] = new String(gids[i].getBytes("ISO-8859-1"), "UTF-8");
                    sql = "delete from usergroup where usergroup_id=" + gids[i];
                    stmt.execute(sql);
                }
                conn.commit();
                conn.setAutoCommit(true);
                TopMenuManager.resetMenus();
                this.msg = "删除成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                try {
                    conn.rollback();
                } catch (Exception connre) {
                }
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }
}
